package cn.fl.ref.fribaseinfo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjCstCorpTeamResumeService;
import cn.fl.ref.fribaseinfo.service.IFundInsSimilarInfoService;
import cn.fl.ref.fribaseinfo.transform.FundInsSimilarInfoVOTransform;
import cn.fl.ref.fribaseinfo.vo.FundInsSimilarInfoVO;
import fl.ref.fribaseinfo.condition.FundInsSimilarInfoQueryCondition;
import fl.ref.fribaseinfo.dto.FundInsSimilarInfoDTO;
import fl.ref.fribaseinfo.service.IFundInsSimilarInfoApiService;

/**
 * @Description: 资管机构相似度信息表
 * @author gengchunlan 
 * @date 20200208
 */
@Service
public class FundInsSimilarInfoServiceImpl implements IFundInsSimilarInfoService {

	private static final Logger logger = LoggerFactory.getLogger(IPrjCstCorpTeamResumeService.class);


	/**
	 * 资管机构相似度信息表
	 */
	@Autowired
	private IFundInsSimilarInfoApiService fundInsSimilarInfoApiService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public FundInsSimilarInfoVO selectById(Long id, String sysCd, String orgCd) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		logger.info("FriBaseInfoVO selectById Long id : " + id);
		FundInsSimilarInfoDTO dto = this.fundInsSimilarInfoApiService.selectById(id, sysCd, orgCd);
		FundInsSimilarInfoVO fundInsSimilarInfoVO = FundInsSimilarInfoVOTransform.toVO(dto);
		return fundInsSimilarInfoVO;
	}

	@Override
	public Long add(FundInsSimilarInfoVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		Long id = fundInsSimilarInfoApiService.add(FundInsSimilarInfoVOTransform.toDTO(entity));
		BaseLogUtils.info(logger, "add", "新增FundInsSimilarInfo数据.", CurrentThreadContext.getCurrentUserName());
		return id;
	}
	@Override
	public boolean modify(FundInsSimilarInfoVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}

		return this.fundInsSimilarInfoApiService.modify(FundInsSimilarInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if (null == page)
			page = new Pager();
		return this.fundInsSimilarInfoApiService.searchListPage(page);
	}

	@Override
	public List<FundInsSimilarInfoVO> searchList(FundInsSimilarInfoQueryCondition condition) {
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		return FundInsSimilarInfoVOTransform.toVOList(this.fundInsSimilarInfoApiService.searchList(condition));
	}

	

	@Override
	public List<KeyValueVO> getKvList(String grpCd) throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),
					CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if (CollectionUtils.isNotEmpty(sysDictionaryDTOList)) {
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "getKvList", "获取字典项组" + grpCd + "失败.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getKvList", "获取字典项组" + grpCd + "失败,系统错误.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return kvList;
	}
	@Override
	public void insertSimilarByBatch(FundInsSimilarInfoVO entity) throws BaseException {
		this.fundInsSimilarInfoApiService.insertSimilarByBatch(FundInsSimilarInfoVOTransform.toDTO(entity));
	}

	@Override
	public void insertSimilarByAppend(FundInsSimilarInfoVO entity) throws BaseException {
		this.fundInsSimilarInfoApiService.insertSimilarByAppend(FundInsSimilarInfoVOTransform.toDTO(entity));
	}

	@Override
	public void changeNum(Map map) throws BaseException {
		this.fundInsSimilarInfoApiService.changeNum(map);
		
	}
}
